<?php

namespace App\Http\Controllers\backend;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Model\Backend\Member;
use App\Model\Backend\ProjectRole;
use App\Model\Backend\Department;
use DB;

use App\Model\Backend\Expense;
use App\Model\Backend\ItemModel;
use App\Model\Backend\ExpenseType;



class MemberController extends Controller
{
	protected $appid = '1105514662';
	protected $token='nVqe3CVXzQrg9Gpw';
	private $return_uri;
	private $access_token;
	private $url = 'http://open.51094.com/user/auth.html';




	public function login(Request $req)
	{


		$Member = Member::where('uid',9)->first();
		$req->session()->put('member',$Member);
		return redirect('/backend/work');
		if($req->input('majia')){
			$user = Member::get();
			return view('backend.layout.login_majia',compact('user'));
		}else{
			if(!session('member')){
				return view('backend.layout.login');
			}else{
				return redirect('backend/work');
			}
		}

	}
//马甲
	function majia(Request $req){

		$Member = Member::where('uid',$req->input('uid'))->first();
		$req->session()->put('member',$Member);
		echo 1;
	}

	//设置
	public function data(Request $req){
		$session = $req->session()->get('member');
		if($session['gid'] != 1) abort(403,'Unauthorized action.');
		return view('backend.comset.index');
	}

	public function index(request $req)
	{

		$code = $req->code;
		$params=array(
				'type'=>'get_user_info',
				'code'=>$code,
				'appid'=>$this->appid,
				'token'=>$this->token
		);

		$res = $this->http( $params );
		$Member = Member::where('token',$res['uniq'])->first();

		if( empty($Member) ){

			$Member = new Member;
			$Member->username = $res['name'];

			$Member->img = $res['img'];
			$Member->token = $res['uniq'];
			$Member->gid = 2;
			$Member->department_id = 1;
			$Member->rid = 1;

			$req->session()->put('member',$Member);

			$Member->save();

		}else{
			$req->session()->put('member',$Member);
		}


		return redirect('/backend/work');
	}
	//QQ api
	private function http( $postfields='', $method='POST', $headers=array()){
		$ci=curl_init();
		curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, FALSE); 
		curl_setopt($ci, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, 30);
		curl_setopt($ci, CURLOPT_TIMEOUT, 30);
		if($method=='POST'){
			curl_setopt($ci, CURLOPT_POST, TRUE);
			if($postfields!='')curl_setopt($ci, CURLOPT_POSTFIELDS, $postfields);
		}
		$headers[]="User-Agent: 51094PHP(open.51094.com)";
		curl_setopt($ci, CURLOPT_HTTPHEADER, $headers);
		curl_setopt($ci, CURLOPT_URL, $this->url);
		$response=curl_exec($ci);
		curl_close($ci);
		$json_r=array();
		if(!empty( $response ))$json_r=json_decode($response, true);
		return $json_r;
	}


//用户列表
	public function memberall()
	{

		$Member_all = Member::get();
		$Member_all = json_encode($Member_all);
		return $Member_all;
		
	}
//修改用户信息
	public function orgStaffMangeSubmit(request $req)
	{

		$id = $req->userId;
		$Member = Member::where('uid',$id)->first();
		$Member->username = $req->name;
		$Member->email = $req->email;
		$Member->wechat = $req->wechat;
		$Member->sex = $req->sex;
		$Member->phone = $req->phone;
		
		$birthday = $req->birthday;
		$birthday = strtotime($birthday);
		$Member->birthday = $birthday;		
		
		$entrytime = $req->joinDate;
		$entrytime = strtotime($entrytime);
		$Member->entrytime = $entrytime;
		$Member->gid = $req->gid;
		
		$Member->save();
		return back();		
	}


	public function logout(Request $req)
	{

		$req->session()->flush();

		return redirect('/');
	}

	public function MangeSubmit(request $req)
	{

			$id = $req->user_id;

			$Member = Member::find($id);
			$Member->username = $req->name;
			$Member->email = $req->email;
			$Member->phone = $req->phone;
			$Member->wechat = $req->wechat;
			$Member->rid = $req->major;
			$Member->sex = $req->sex;

			$birthday = $req->birthday;
			$birthday = strtotime($birthday);
			$Member->birthday = $birthday;		

			$entrytime = $req->joinDate;
			$entrytime = strtotime($entrytime);
			$Member->entrytime = $entrytime;
			$Member->gid = $req->gid;
			//dd($Member);
			$Member->save();
			return back();


	}

	public function memberdata(request $req)
	{


		$data = Member::find($req->id);
		//dd($data);
		$data->entrytime = date('Y-m-d',$data->entrytime);
		$data->birthday = date('Y-m-d',$data->birthday);

		return json_encode($data);
	}
	
	public function personal(request $req)
	{
		
		 if( $req->isMethod('post') ){
		 	//dd($req);
		 	$id = $req->user_id;
		 	$Member = Member::find($id);
		 	$Member->username = $req->user_username;
		 	$Member->wechat = $req->user_wechat;
		 	$Member->sex = $req->user_sex;
		 	$Member->phone = $req->user_mobile;
		 	$Member->email = $req->email;

		 	$Member->save();
		 	return view('backend.member.personal',compact('Member'));
		 }else{

		 	$id=session()->get('member')['uid'];

			$Member = Member::where('uid',$id)->first();
			//dd($Member);
			return view('backend.member.personal',compact('Member'));
		 }
	}
	
	function task(){


		$id=session()->get('member')['uid'];
    	$expense = Expense::where('uid', $id)->paginate(3);


	    //循环并且压入对应的中文名
	    foreach( $expense as $k=>$v ){

	        $i = Member::find($v->uid);


	        $t = ItemModel::find($v->did);

    if( $v->did == -1 ){
        $t = '无关联项目'; 
        $expense[$k]['itemname'] = $t;
    }else{

        $t = ItemModel::find($v->did);     

        $expense[$k]['itemname'] = $t->item_name;   
    }


	        $e = ExpenseType::find($v->type);
	  
	        $expense[$k]['username'] = $i->username;
	        //$expense[$k]['itemname'] = $t->item_name;
	        $expense[$k]['expense_type'] = $e->expense_type;
	        $expense[$k]['time'] = date('Y/m/d',$v->time);


	    }    	

		
		$member = [];
		$memberArr = Member::get();
		$uid=session()->get('member')['uid'];
		foreach($memberArr as $m){
			$member[$m['uid']] = $m['username'];
		}
		
		$task = DB::select('select t.*, i.member_id from xz_items i, xz_items_tasks t where i.id=t.item_id and t.member_id_task='. $uid .' and task_status in(30,31)');
		
		//dd($expense);
		return view('backend.member.task', compact('task', 'member','expense'));

	}


}
